﻿// <copyright file="Settings.xaml.cs" company="Snowman Consulting">
//     Copyright (c) 2009 Snowman Consulting. All rights reserved.
// </copyright>

namespace SnowmanConsulting.ProductKeyManager.UI
{
    using System.Reflection;
    using System.Windows;
    using SnowmanConsulting.ProductKeyManager.Logging;

    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class Settings : Window
    {
        /// <summary>
        /// sort directions
        /// </summary>
        private SortDirection[] sortDirections = 
        {
            SortDirection.None,
            SortDirection.Ascending,
            SortDirection.Descending
        };

        /// <summary>
        /// Initializes a new instance of the <see cref="Settings"/> class.
        /// </summary>
        public Settings()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Assembly assm = Assembly.GetExecutingAssembly();
            mLogFile.Text = assm.Location.Replace("ProductKeyManager.exe", Logger.LogFile);

            this.chkSaveLastSearch.IsChecked = Properties.Settings.Default.SaveLastSearch;
            this.chkEnableXsl.IsChecked = Properties.Settings.Default.EnableXslExport;

            this.cmbSortName.ItemsSource = this.sortDirections;
            this.cmbSortName.SelectedItem = Properties.Settings.Default.SortNameDirection;

            this.cmbSortKeyCount.ItemsSource = this.sortDirections;
            this.cmbSortKeyCount.SelectedItem = Properties.Settings.Default.SortKeyCountDirection;
        }

        /// <summary>
        /// Handles the Click event of the SettingsApply control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void SettingsApply_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.SaveLastSearch = (bool)this.chkSaveLastSearch.IsChecked;
            Properties.Settings.Default.EnableXslExport = (bool)this.chkEnableXsl.IsChecked;

            Properties.Settings.Default.SortKeyCountDirection = (SortDirection)this.cmbSortKeyCount.SelectedItem;
            Properties.Settings.Default.SortNameDirection = (SortDirection)this.cmbSortName.SelectedItem;

            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// Handles the Click event of the SettingsCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void SettingsCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the SettingsOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void SettingsOk_Click(object sender, RoutedEventArgs e)
        {
            this.SettingsApply_Click(sender, e);
            this.Close();
        }
    }
}
